﻿using Treca.Base_SysManage.Entity;
using Treca.Business.Base_SysManage;
using Treca.PushMessage.Business;
using Treca.Util;
using Treca.Util.PublishSubscribe;
using EFCore.Sharding;
#if EFCORE5
using LinqKit;
#endif
using Microsoft.AspNetCore.SignalR;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using Senparc.Weixin.MP.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Treca.PushMessage.Base.MessageEvent
{
  
    
    public class MessageEvent : IEventHandler<WeiXinApp>
    {
        private string[] Getunit(string value, string regx)
        {
            if (string.IsNullOrWhiteSpace(value))
                return null;
            bool isMatch = Regex.IsMatch(value, regx);
            if (!isMatch)
                return null;
            MatchCollection matchCol = Regex.Matches(value, regx);
            string[] result = new string[matchCol.Count];
            if (matchCol.Count > 0)
            {
                for (int i = 0; i < matchCol.Count; i++)
                {
                    result[i] = matchCol[i].Value;
                }
            }
            return result;
        }
        Ims_messageserviceBusiness _MessageserviceBusiness { get => AutofacHelper.GetScopeService<Ims_messageserviceBusiness>(); }
        Ims_messagetempleteBusiness _MessagetempleteBusiness { get => AutofacHelper.GetScopeService<Ims_messagetempleteBusiness>(); }
        public AjaxResult Run(string Command, WeiXinApp T)
        {
            var service = _MessageserviceBusiness.GetIQueryable().Where(t=>t.appid == T.appid).FirstOrDefault();
            if (service.IsNullOrEmpty())
            {
                return AjaxHelper.Error("消息服务不存在");
            }
            var templete = _MessagetempleteBusiness.GetIQueryable().Where(t => t.templeteId == T.templeteId&& t.serviceId == service.Id).FirstOrDefault();
            if (templete.IsNullOrEmpty())
            {
                return AjaxHelper.Error("消息模板不存在");
            }

            //string[] units = Getunit(templete.templetecontent, "(?<=(#))[.\\s\\S]*?(?=(#))");
            //units.ForEach((unit) =>
            //{
            //    var item = unit.Replace("#", "");
            //    if (T.messageobj.ContainsProperty(item))
            //    {
            //        base_linkage.filedcondition = base_linkage.filedcondition.Replace("#" + item + "#", theData.GetPropertyValue(item).ToString());
            //    }
            //});
            string access_token = AccessTokenContainer.GetAccessTokenResult(service.appid).access_token;
            TempleteModel_MiniProgram templeteModel_Mini = new TempleteModel_MiniProgram();
            templeteModel_Mini.appid = service.appid;
            var res = TemplateApi.SendTemplateMessage(access_token,T.openId, templete.templeteId,"",T.messageobj, templeteModel_Mini, 10000);
            if (res.errcode == 0)
            {
                return AjaxHelper.Success();
            }
            else
            {
                return AjaxHelper.Error(res.errmsg);
            }
        }

        public Task<AjaxResult> RunAsync(string Command, WeiXinApp T)
        {
            throw new NotImplementedException();
        }
    }
}
